package com.bdqn.controller.mgr;

import com.alibaba.fastjson.JSON;
import com.bdqn.constant.Constants;
import com.bdqn.constant.PageSupport;
import com.bdqn.pojo.*;
import com.bdqn.service.common.AppCategoryService;
import com.bdqn.service.common.AppInfoService;
import com.bdqn.service.common.AppVersionService;
import com.bdqn.service.common.DataDictionaryService;
import com.bdqn.service.mgr.BackendUserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/mgr")
public class BackendUserController {
    private Logger logger = Logger.getLogger(BackendUserController.class);

    @Resource
    private BackendUserService backendUserService;
    @Resource
    private DataDictionaryService dataDictionaryService;
    @Resource
    private AppCategoryService appCategoryService;
    @Resource
    private AppInfoService appInfoService;
    @Resource
    private AppVersionService appVersionService;
    @RequestMapping("/login")
    public String toLogin(){
        logger.info("进入后台管理登录");
        return "/backendlogin";
    }

    @PostMapping("/doLogin")
    public String doLogin(HttpSession session,
                          @RequestParam("userCode") String userCode,
                          @RequestParam("userPassword")String userPassword){
        logger.info("进行后台管理登录");
        logger.info("账号：" + userCode + "\t密码：" + userPassword);
        BackendUser backendUser = backendUserService.login(userCode,userPassword);
        if (backendUser != null) {
            session.setAttribute(Constants.userSession,backendUser);
            return "backend/main";
        }
        return "backendlogin";
    }

    @GetMapping("/backend/app/list")
    public String list(Model model,
                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "querySoftwareName", required = false) String softwareName,
                       @RequestParam(value = "queryFlatformId", required = false) Long flatformId,
                       @RequestParam(value = "queryCategoryLevel1", required = false) Long categoryLevel1,
                       @RequestParam(value = "queryCategoryLevel2", required = false) Long categoryLevel2,
                       @RequestParam(value = "queryCategoryLevel3", required = false) Long categoryLevel3){
        logger.info("进入查询列表方法");
        List<DataDictionary> flatFormList = dataDictionaryService.flatFormList();
        logger.info("所属平台：" + flatFormList + "\n");
        model.addAttribute("flatFormList", flatFormList);
        List<AppCategory> categoryLevel1List = appCategoryService.categoryLevel1List(null);
        logger.info("一级分类：" + categoryLevel1List + "\n");
        model.addAttribute("categoryLevel1List", categoryLevel1List);
        List<AppInfo> appInfoList = new ArrayList<>();
        //创建一个Page对象
        PageSupport pages = new PageSupport();
        try {
            //设置页面容量
            Integer pageSize = Constants.pageSize;
            pages.setPageSize(pageSize);
            logger.info("页面容量：" + pageSize);
            //获取当前页码
            pages.setCurrentPageNo(pageIndex);
            logger.info("当前页码：" + pageIndex);
            //获取当前总数据,（并获取总页数）
            pages.setTotalCount(appInfoService.pageCountMgr(softwareName, flatformId, categoryLevel1,
                    categoryLevel2, categoryLevel3, pageIndex, pageSize));

            logger.info("总数据：" + pages.getTotalCount() + "\n总页码：" + pages.getTotalPageCount());
            logger.info("当前页码：" + pageIndex);
            //控制首页和尾页
            if (pageIndex > pages.getTotalPageCount()) {
                pageIndex = pages.getTotalPageCount();
                throw new RuntimeException("页码不正确");
            }
            appInfoList = appInfoService.appList(softwareName, flatformId, categoryLevel1,
                    categoryLevel2, categoryLevel3, pageIndex, pageSize);
            for (AppInfo appInfo: appInfoList){
                logger.info("手机信息id:" + appInfo.getId());
                logger.info("版本id:" + appInfo.getVersionId());
                logger.info("状态Id:" + appInfo.getStatus());
            }
        }catch (Exception e){
            logger.info(e);
        }
        model.addAttribute("pages",pages);
        model.addAttribute("appInfoList",appInfoList);
        model.addAttribute("querySoftwareName",softwareName);
        model.addAttribute("queryFlatformId",flatformId);
        model.addAttribute("queryCategoryLevel1",categoryLevel1);
        model.addAttribute("queryCategoryLevel2",categoryLevel2);
        model.addAttribute("queryCategoryLevel3",categoryLevel3);
        return "backend/applist";
    }
    @ResponseBody
    @GetMapping("/backend/app/categorylist")
    public Object categorylist(@RequestParam("pid")Long pid){
        logger.info("分类ID：" + pid + "\n");
        List<AppCategory> categoryLevelList = appCategoryService.categoryLevel1List(pid);
        return JSON.toJSONString(categoryLevelList);
    }

    @RequestMapping("/backend/app/check")
    public String check(Model model,@RequestParam("aid")Long aid,@RequestParam("vid")Long vid){
        logger.info("进入审查方法");
        logger.info("手机信息ID：" + aid);
        AppInfo appInfo = appInfoService.getApp(aid);
        logger.info("版本信息ID：" + vid);
        AppVersion appVersion = appVersionService.getVersionById(vid);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("appVersion",appVersion);
        return "backend/appcheck";
    }

//    /backend/app/checksave 审查修改路径
    @PostMapping("/backend/app/checksave")
    public String checksave(@RequestParam("id") Long id,@RequestParam("status")Long status){
        logger.info("进入审查是否通过方法");
        logger.info("审查返回值：" + status);
        logger.info("手机Id：" + id);
        if (appInfoService.updateById(id,status)){
            return "redirect:/mgr/backend/app/list";
        }
        return "backend/appcheck";
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/logout")
    public String logOut(){
        logger.info("退出登录");
        return "/backendlogin";
    }


}
